<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="省份" prop="province">
        <el-input
          v-model="queryParams.province"
          placeholder="请输入省份"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地市" prop="city">
        <el-input
          v-model="queryParams.city"
          placeholder="请输入地市"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区县" prop="county">
        <el-input
          v-model="queryParams.county"
          placeholder="请输入区县"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="难度" prop="difficulty">
        <el-select v-model="queryParams.difficulty" placeholder="请选择难度" clearable>
          <el-option
            v-for="dict in dict.type.dest_diff_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="海拔" prop="maxAltitude">
        <el-input
          v-model="queryParams.maxAltitude"
          placeholder="请输入海拔(单位：米)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="行程" prop="hikingDistance">
        <el-input
          v-model="queryParams.hikingDistance"
          placeholder="请输入行程(单位：公里)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="拔高" prop="elevationGain">
        <el-input
          v-model="queryParams.elevationGain"
          placeholder="请输入拔高(单位：米)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否启用" prop="enabled">
        <el-select v-model="queryParams.enabled" placeholder="请选择是否启用" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="创建人ID" prop="createdById">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.createdById"-->
      <!--          placeholder="请输入创建人ID"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="创建人" prop="createdBy">
        <el-input
          v-model="queryParams.createdBy"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createdTime">
        <el-date-picker clearable
                        v-model="queryParams.createdTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <!--      <el-form-item label="更新人ID" prop="updatedById">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.updatedById"-->
      <!--          placeholder="请输入更新人ID"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="更新人" prop="updatedBy">
        <el-input
          v-model="queryParams.updatedBy"
          placeholder="请输入更新人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新时间" prop="updatedTime">
        <el-date-picker clearable
                        v-model="queryParams.updatedTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['kk:destination:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['kk:destination:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['kk:destination:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['kk:destination:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="destinationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="唯一主键" align="center" prop="id" />-->
      <el-table-column label="省份" align="center" prop="province"/>
      <el-table-column label="地市" align="center" prop="city"/>
      <el-table-column label="区县" align="center" prop="county"/>
      <el-table-column label="名称" align="center" prop="name"/>
      <el-table-column label="难度" align="center" prop="difficulty">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dest_diff_level" :value="scope.row.difficulty"/>
        </template>
      </el-table-column>
      <el-table-column label="海拔(米)" align="center" prop="maxAltitude"/>
      <el-table-column label="行程（千米）" align="center" prop="hikingDistance"/>
      <el-table-column label="拔高（米）" align="center" prop="elevationGain"/>
      <el-table-column label="描述" align="center" prop="description"/>
      <el-table-column label="是否启用" align="center" prop="enabled">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.enabled"/>
        </template>
      </el-table-column>
      <!--      <el-table-column label="创建人ID" align="center" prop="createdById" />-->
      <el-table-column label="创建人" align="center" prop="createdBy"/>
      <el-table-column label="创建时间" align="center" prop="createdTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="更新人ID" align="center" prop="updatedById" />-->
      <el-table-column label="更新人" align="center" prop="updatedBy"/>
      <el-table-column label="更新时间" align="center" prop="updatedTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['kk:destination:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['kk:destination:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改徒步目的地信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="省份" prop="province">
          <el-input v-model="form.province" placeholder="请输入省份"/>
        </el-form-item>
        <el-form-item label="地市" prop="city">
          <el-input v-model="form.city" placeholder="请输入地市"/>
        </el-form-item>
        <el-form-item label="区县" prop="county">
          <el-input v-model="form.county" placeholder="请输入区县"/>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称"/>
        </el-form-item>
        <el-form-item label="难度" prop="difficulty">
          <el-select v-model="form.difficulty" placeholder="请选择难度">
            <el-option
              v-for="dict in dict.type.dest_diff_level"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="海拔" prop="maxAltitude">
          <el-input v-model="form.maxAltitude" placeholder="请输入海拔(单位：米)"/>
        </el-form-item>
        <el-form-item label="行程" prop="hikingDistance">
          <el-input v-model="form.hikingDistance" placeholder="请输入行程(单位：公里)"/>
        </el-form-item>
        <el-form-item label="拔高" prop="elevationGain">
          <el-input v-model="form.elevationGain" placeholder="请输入拔高(单位：米)"/>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="是否启用" prop="enabled">
          <el-select v-model="form.enabled" placeholder="请选择是否启用">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="创建人ID" prop="createdById">-->
        <!--          <el-input v-model="form.createdById" placeholder="请输入创建人ID" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="创建人" prop="createdBy">-->
        <!--          <el-input v-model="form.createdBy" placeholder="请输入创建人" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="创建时间" prop="createdTime">-->
        <!--          <el-date-picker clearable-->
        <!--            v-model="form.createdTime"-->
        <!--            type="date"-->
        <!--            value-format="yyyy-MM-dd"-->
        <!--            placeholder="请选择创建时间">-->
        <!--          </el-date-picker>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="更新人ID" prop="updatedById">-->
        <!--          <el-input v-model="form.updatedById" placeholder="请输入更新人ID" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="更新人" prop="updatedBy">-->
        <!--          <el-input v-model="form.updatedBy" placeholder="请输入更新人" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="更新时间" prop="updatedTime">-->
        <!--          <el-date-picker clearable-->
        <!--            v-model="form.updatedTime"-->
        <!--            type="date"-->
        <!--            value-format="yyyy-MM-dd"-->
        <!--            placeholder="请选择更新时间">-->
        <!--          </el-date-picker>-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listDestination, getDestination, delDestination, addDestination, updateDestination} from "@/api/kk/destination"

export default {
  name: "Destination",
  dicts: ['sys_yes_no', 'dest_diff_level'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 徒步目的地信息表格数据
      destinationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        province: null,
        city: null,
        county: null,
        name: null,
        difficulty: null,
        maxAltitude: null,
        hikingDistance: null,
        elevationGain: null,
        description: null,
        enabled: null,
        createdById: null,
        createdBy: null,
        createdTime: null,
        updatedById: null,
        updatedBy: null,
        updatedTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        province: [
          {required: true, message: "省份不能为空", trigger: "blur"}
        ],
        city: [
          {required: true, message: "地市不能为空", trigger: "blur"}
        ],
        county: [
          {required: true, message: "区县不能为空", trigger: "blur"}
        ],
        name: [
          {required: true, message: "名称不能为空", trigger: "blur"}
        ],
        difficulty: [
          {required: true, message: "难度不能为空", trigger: "change"}
        ],
        maxAltitude: [
          {required: true, message: "海拔(单位：米)不能为空", trigger: "blur"}
        ],
        hikingDistance: [
          {required: true, message: "行程(单位：公里)不能为空", trigger: "blur"}
        ],
        elevationGain: [
          {required: true, message: "拔高(单位：米)不能为空", trigger: "blur"}
        ],
        enabled: [
          {required: true, message: "是否启用不能为空", trigger: "change"}
        ],
        createdById: [
          {required: true, message: "创建人ID不能为空", trigger: "blur"}
        ],
        createdBy: [
          {required: true, message: "创建人不能为空", trigger: "blur"}
        ],
        createdTime: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询徒步目的地信息列表 */
    getList() {
      this.loading = true
      listDestination(this.queryParams).then(response => {
        this.destinationList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        province: null,
        city: null,
        county: null,
        name: null,
        difficulty: null,
        maxAltitude: null,
        hikingDistance: null,
        elevationGain: null,
        description: null,
        enabled: null,
        createdById: null,
        createdBy: null,
        createdTime: null,
        updatedById: null,
        updatedBy: null,
        updatedTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加徒步目的地信息"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getDestination(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改徒步目的地信息"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDestination(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addDestination(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除徒步目的地信息编号为"' + ids + '"的数据项？').then(function () {
        return delDestination(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('kk/destination/export', {
        ...this.queryParams
      }, `destination_${new Date().getTime()}.xlsx`)
    },

    /*KK自定义函数*/
    selectProvince() {
      this.$.modal.open("弹窗测试", '')
    }
  }
}
</script>
